<template>
  <div class="orders">
    <!-- 面包屑导航栏 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item>
        <a href="/hello">首页</a>
      </el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <el-row>
        <el-col :span="8">
          <!-- 查询功能 -->
          <el-input v-model="queryInfo.query" placeholder="请输入查询内容">
            <el-button slot="append" @click="shreSerch" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <el-table :data="orderList" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="订单编号" prop="order_number" width="250px"></el-table-column>
        <el-table-column label="订单价格" prop="order_price"></el-table-column>
        <el-table-column label="是否付款">
          <template slot-scope="scope">
            <el-tag effect="dark" size="mini" type="danger" v-if="scope.row.order_pay==0">未付款</el-tag>
            <el-tag effect="dark" size="mini" type="success" v-else-if="scope.row.order_pay==1">已付款</el-tag>
            <el-tag effect="dark" size="mini" type="warning" v-else>订单异常</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否发货" prop="is_send"></el-table-column>
        <el-table-column label="下单时间" prop="create_time"></el-table-column>
        <el-table-column label="操作">
          <!-- 操作 -->
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" content="修改订单地址" placement="top">
              <el-button
                @click="changeCityPath(scope.row)"
                type="primary"
                size="mini"
                icon="el-icon-edit"
              ></el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="物流信息" placement="top">
              <el-button
                @click="setInfo()"
                type="success"
                size="mini"
                icon="el-icon-location-outline"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 编辑地址 -->

    <el-dialog title="修改地址" @close="closeFrom" :visible.sync="dialogVisiblePath" width="50%">
      <el-form :model="pathData" label-width="100px" ref="path" :rules="pathRules">
        <el-form-item label="省市区/县" prop="paht">
          <el-cascader
            style="width:100%"
            v-model="pathData.paht"
            :options="regionData"
            :props="props"
            @change="handleChange"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="pathInfo">
          <el-input v-model="pathData.pathInfo"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisiblePath = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisiblePath = false">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 物流信息 -->
    <el-dialog title="物流进度" :visible.sync="logisticsDialogVisible" width="50%">
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in activities"
          :key="index"
          :timestamp="activity.ftime"
        >{{activity.context}}</el-timeline-item>
      </el-timeline>
      <span slot="footer" class="dialog-footer">
        <el-button @click="logisticsDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="logisticsDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// 处理时间日期
import moment from 'moment'
// 处理地址
import { regionData } from 'element-china-area-data'
import { GetOrder, OrderPath } from '@/api/api.js'
export default {
  name: 'BackShopOrders',

  data () {
    return {
      // 订单列表
      orderList: [],
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      total: 0,
      // 市区
      regionData: regionData,
      // 地址show
      dialogVisiblePath: false,
      // 修改地址校验
      pathRules: {
        path: [
          { required: true, message: '请输入省市区/县', trigger: 'blur' },
        ],
        pathInfo: [
          { required: true, message: '请输入详细地址', trigger: 'blur' },
        ]
      },
      // 地址数据
      pathData: {
        paht: [],
        pathInfo: ''
      },
      // 下拉菜单城市列表规则
      props: {
        expandTrigger: 'hover',
        label: 'label',
        value: 'value',
        children: 'children'
      },
      // 物流显示与隐藏
      logisticsDialogVisible: false,
      activities: []
    };
  },

  created () {
    this.getOrderList()
  },

  methods: {
    // 获取商品订单列表
    async getOrderList () {
      const { data: res } = await GetOrder({ params: this.queryInfo })
      if (res.meta.status !== 200) return this.$message.error('请求订单列表失败');
      // 处理时间
      res.data.goods.forEach(item => {
        item.create_time = moment(item.create_time).format('YYYY-MM-DD HH:MM:SS')
      })
      this.orderList = res.data.goods
      this.total = res.data.total
    },
    // 处理显示几页
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize;
      this.getOrderList()
    },
    // 处理几条数据
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage;
      this.getOrderList()
    },
    // 搜索数据
    shreSerch () {
      if (this.queryInfo.query.trim().length === 0) return this.$message.info('查询订单失败')
      this.getOrderList()
    },
    // 修改订单地址
    changeCityPath (row) {
      // console.log(row);
      this.dialogVisiblePath = true
    },
    // 处理城市change
    handleChange () {
    },
    // 关闭修改地址校验
    closeFrom () {
      this.$refs.path.resetFields()
    },
    // 订单地址
    async setInfo () {
      this.logisticsDialogVisible = true;
      const { data: res } = await OrderPath()
      if (res.meta.status !== 200) return this.$message.error('请求物流详情失败')
      // 赋值物流信息
      this.activities = res.data
    }
  },
};
</script>

<style lang="less" scoped>
</style>